<!DOCTYPE html>












  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">









  
  
    
  
  <link rel="stylesheet" media="all" href="https://cdnjs.cloudflare.com/ajax/libs/Han/3.3.0/han.min.css">




<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">



















  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.3.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '6.3.0',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="基本语法echo 命令脚本中会大量用到echo命令输出执行日志 12345678# 单行$ echo hello worldhello world# 多行$ echo &quot;helloworld&quot; 常用参数-n默认情况下，echo输出的文本末尾会有一个回车符。-n参数可以取消末尾的回车符，使得下一个提示符紧跟在输出内容的后面。 123456789$ echo -n hello worldhello w">
<meta name="keywords" content="linux,shell">
<meta property="og:type" content="article">
<meta property="og:title" content="shell">
<meta property="og:url" content="http://codertang.com/2019/01/10/shell/index.html">
<meta property="og:site_name" content="Tonny&#39;s Blog">
<meta property="og:description" content="基本语法echo 命令脚本中会大量用到echo命令输出执行日志 12345678# 单行$ echo hello worldhello world# 多行$ echo &quot;helloworld&quot; 常用参数-n默认情况下，echo输出的文本末尾会有一个回车符。-n参数可以取消末尾的回车符，使得下一个提示符紧跟在输出内容的后面。 123456789$ echo -n hello worldhello w">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2021-12-08T07:05:29.911Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="shell">
<meta name="twitter:description" content="基本语法echo 命令脚本中会大量用到echo命令输出执行日志 12345678# 单行$ echo hello worldhello world# 多行$ echo &quot;helloworld&quot; 常用参数-n默认情况下，echo输出的文本末尾会有一个回车符。-n参数可以取消末尾的回车符，使得下一个提示符紧跟在输出内容的后面。 123456789$ echo -n hello worldhello w">






  <link rel="canonical" href="http://codertang.com/2019/01/10/shell/">



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>shell | Tonny's Blog</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tonny's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">莫等闲, 白了少年头, 空悲切</p>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>
  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://codertang.com/2019/01/10/shell/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tonny Yi">
      <meta itemprop="description" content="Coding, Life, Zen">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tonny's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">shell
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-01-10 08:36:03" itemprop="dateCreated datePublished" datetime="2019-01-10T08:36:03+08:00">2019-01-10</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2021-12-08 15:05:29" itemprop="dateModified" datetime="2021-12-08T15:05:29+08:00">2021-12-08</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/linux/shell/" itemprop="url" rel="index"><span itemprop="name">shell</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <h1 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</h1><h2 id="echo-命令"><a href="#echo-命令" class="headerlink" title="echo 命令"></a>echo 命令</h2><p>脚本中会大量用到echo命令输出执行日志</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 单行</span></span><br><span class="line">$ <span class="built_in">echo</span> hello world</span><br><span class="line">hello world</span><br><span class="line"></span><br><span class="line"><span class="comment"># 多行</span></span><br><span class="line">$ <span class="built_in">echo</span> <span class="string">"hello</span></span><br><span class="line"><span class="string">world</span></span><br><span class="line"><span class="string">"</span></span><br></pre></td></tr></table></figure>
<h2 id="常用参数"><a href="#常用参数" class="headerlink" title="常用参数"></a>常用参数</h2><h3 id="n"><a href="#n" class="headerlink" title="-n"></a><code>-n</code></h3><p>默认情况下，<code>echo</code>输出的文本末尾会有一个回车符。<code>-n</code>参数可以取消末尾的回车符，使得下一个提示符紧跟在输出内容的后面。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">echo</span> -n hello world</span><br><span class="line">hello world$</span><br><span class="line"></span><br><span class="line">$ <span class="built_in">echo</span> a;<span class="built_in">echo</span> b</span><br><span class="line">a</span><br><span class="line">b</span><br><span class="line"></span><br><span class="line">$ <span class="built_in">echo</span> -n a;<span class="built_in">echo</span> b</span><br><span class="line">ab</span><br></pre></td></tr></table></figure>
<p><code>-n</code>参数可以让两个<code>echo</code>命令的输出连在一起，出现在同一行。</p>
<h2 id="e"><a href="#e" class="headerlink" title="-e"></a><code>-e</code></h2><p><code>-e</code>参数会解释引号（双引号和单引号）里面的特殊字符（比如换行符<code>\n</code>）。如果不使用<code>-e</code>参数，即默认情况下，引号会让特殊字符变成普通字符，<code>echo</code>不解释它们，原样输出。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">echo</span> <span class="string">"Hello\nWorld"</span></span><br><span class="line">Hello\nWorld</span><br><span class="line"></span><br><span class="line">$ <span class="built_in">echo</span> -e <span class="string">"Hello\nWorld"</span></span><br><span class="line">Hello</span><br><span class="line">World</span><br><span class="line"></span><br><span class="line">$ <span class="built_in">echo</span> -e <span class="string">'Hello\nWorld'</span></span><br><span class="line">Hello</span><br><span class="line">World</span><br></pre></td></tr></table></figure>
<h1 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h1><h2 id="声明变量"><a href="#声明变量" class="headerlink" title="声明变量"></a>声明变量</h2><p>变量声明方式: <code>变量名=变量值</code>, <code>=</code>左右不要有空格. 变量名命名规则:</p>
<ul>
<li>只能使用数字, 大小写字母, 下划线, 首字符不能为数字</li>
<li>不能有空格, 可以使用<code>_</code></li>
<li>不能使用base里的关键字(使用<code>help</code>命令查看关键字)</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">boo</span><br><span class="line">_var=<span class="string">"hello"</span></span><br><span class="line"><span class="comment"># 除了显示设置变量, 还可以用语句给变量赋值</span></span><br><span class="line"> file <span class="keyword">in</span> `ls /etc`</span><br><span class="line">或</span><br><span class="line"><span class="keyword">for</span> file <span class="keyword">in</span> $(ls /etc)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用变量只需要在变量名前加美元符号即可</span></span><br><span class="line">your_name=<span class="string">"Tom"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$your_name</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;your_name&#125;</span></span><br></pre></td></tr></table></figure>
<p><strong>推荐给所有变量加上花括号</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">your=<span class="string">"Tom"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$your_name</span>   <span class="comment"># 找不到变量 your_name</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;your&#125;</span>_name  <span class="comment"># 输出拼接字符串 Tom_name</span></span><br></pre></td></tr></table></figure>
<p>已定义的变量, 可以被重新赋值, 如:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">your_name=<span class="string">"John"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$your_name</span></span><br><span class="line">your_name=<span class="string">"alibaba"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$your_name</span></span><br></pre></td></tr></table></figure>
<p>第二次赋值的时候不能写\$your_name=”alibaba”, <strong>使用变量的时候才加美元符（$）</strong></p>
<h2 id="只读变量"><a href="#只读变量" class="headerlink" title="只读变量"></a>只读变量</h2><p>使用 readonly 命令可以将变量定义为只读变量, 只读变量的值不能被改变.<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line">myUrl=<span class="string">"http://www.google.com"</span></span><br><span class="line"><span class="built_in">readonly</span> myUrl</span><br><span class="line">myUrl=<span class="string">"http://www.runoob.com"</span></span><br></pre></td></tr></table></figure></p>
<p>运行时会报错<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/bin/sh: NAME: This variable is <span class="built_in">read</span> only.</span><br></pre></td></tr></table></figure></p>
<h2 id="删除变量"><a href="#删除变量" class="headerlink" title="删除变量"></a>删除变量</h2><p>使用 <code>unset</code> 命令可以删除变量<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/sh</span></span><br><span class="line">myUrl=<span class="string">"http://www.runoob.com"</span></span><br><span class="line"><span class="built_in">unset</span> myUrl</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$myUrl</span></span><br></pre></td></tr></table></figure></p>
<p>以上实例执行将没有任何输出</p>
<h2 id="变量替换"><a href="#变量替换" class="headerlink" title="变量替换"></a>变量替换</h2><p>变量替换是指可以根据变量的状态(是否为空, 是否定义等)来改变变量的值</p>
<table>
<thead>
<tr>
<th style="text-align:center">形式</th>
<th style="text-align:left">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">${var}</td>
<td style="text-align:left">变量本来的值</td>
</tr>
<tr>
<td style="text-align:center">${var:+word}</td>
<td style="text-align:left">如果变量var被定义, 那么返回word, 但不改变变量var的值</td>
</tr>
<tr>
<td style="text-align:center">${var:-word}</td>
<td style="text-align:left">变量var如果为空或已被删除(unset), 则返回word, 但不改变变量var的值</td>
</tr>
<tr>
<td style="text-align:center">${var:=word}</td>
<td style="text-align:left">变量var如果为空或已被删除(unset), 则返回word, 同时将变量var的值设置为word</td>
</tr>
<tr>
<td style="text-align:center">${var:?msg}</td>
<td style="text-align:left">变量var如果为空或已被删除(unset), 则将msg发送到标识错误输出. 若此替换出现在脚本中, 则脚本停止运行</td>
</tr>
</tbody>
</table>
<p>示例:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;var:-"Variable is not set"&#125;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"1 - Value of var is <span class="variable">$&#123;var&#125;</span>"</span>	</span><br><span class="line"><span class="comment"># Variable is not set</span></span><br><span class="line"><span class="comment"># 1 - Value of var is</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;var:="Variable is not set"&#125;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"2 - Value of var is <span class="variable">$&#123;var&#125;</span>"</span></span><br><span class="line"><span class="comment"># Variable is not set</span></span><br><span class="line"><span class="comment"># 2 - Value of var is Variable is not set</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">unset</span> var</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;var:+"This is default value"&#125;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"3 - Value of var is <span class="variable">$var</span>"</span></span><br><span class="line"><span class="comment"># </span></span><br><span class="line"><span class="comment"># 3 - Value of var is</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">var=<span class="string">"Prefix"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;var:+"This is default value"&#125;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"4 - Value of var is <span class="variable">$var</span>"</span></span><br><span class="line"><span class="comment"># This is default value</span></span><br><span class="line"><span class="comment"># 4 - Value of var is Prefix</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;var:?"Print this message"&#125;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"5 - Value of var is <span class="variable">$&#123;var&#125;</span>"</span></span><br><span class="line"><span class="comment"># Prefix</span></span><br><span class="line"><span class="comment"># 5 - Value of var is Prefix</span></span><br></pre></td></tr></table></figure>
<h2 id="变量类型"><a href="#变量类型" class="headerlink" title="变量类型"></a>变量类型</h2><ul>
<li><strong>局部变量</strong> 局部变量在脚本或命令中定义，仅在当前shell实例中有效，其他shell启动的程序不能访问局部变量。</li>
<li><strong>环境变量</strong> 所有的程序，包括shell启动的程序，都能访问环境变量，有些程序需要环境变量来保证其正常运行。必要的时候shell脚本也可以定义环境变量。</li>
<li><strong>shell变量</strong> shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量，有一部分是局部变量，这些变量保证了shell的正常运行</li>
</ul>
<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><p><a href="https://blog.csdn.net/dongwuming/article/details/50605911" target="_blank" rel="noopener">linux shell 字符串操作详解 （长度，读取，替换，截取，连接，对比，删除，位置 ）</a><br><a href="https://stackoverflow.com/questions/11426529/reading-output-of-a-command-into-an-array-in-bash" target="_blank" rel="noopener">Reading output of a command into an array in Bash</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">your_name=<span class="string">'runoob'</span></span><br><span class="line">str=<span class="string">"Hello, I know you are \"<span class="variable">$your_name</span>\"! \n"</span></span><br><span class="line"><span class="built_in">echo</span> -e <span class="variable">$str</span></span><br></pre></td></tr></table></figure>
<p>输出结果<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Hello, I know you are &quot;runoob&quot;!</span><br></pre></td></tr></table></figure></p>
<p>单引号字符串的限制：</p>
<ul>
<li>单引号里的任何字符都会原样输出，单引号字符串中的变量是无效的；</li>
<li>单引号字串中不能出现单独一个的单引号（对单引号使用转义符后也不行），但可成对出现，作为字符串拼接使用。</li>
</ul>
<p>双引号的优点：</p>
<ul>
<li>双引号里可以有变量</li>
<li>双引号里可以出现转义字符</li>
</ul>
<h3 id="操作"><a href="#操作" class="headerlink" title="操作"></a>操作</h3><table>
<thead>
<tr>
<th style="text-align:center">表达式</th>
<th style="text-align:center">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>${#string}</code></td>
<td style="text-align:center"><code>$string</code>的长度</td>
</tr>
<tr>
<td style="text-align:center"><code>${string:position}</code></td>
<td style="text-align:center">在<code>$string</code>中, 从位置<code>$position</code>开始提取子串</td>
</tr>
<tr>
<td style="text-align:center"><code>${string:position:length}</code></td>
<td style="text-align:center">在<code>$string</code>中, 从位置<code>$position</code>开始提取长度为<code>$length</code>的子串</td>
</tr>
<tr>
<td style="text-align:center"><code>${string#substring}</code></td>
<td style="text-align:center">从变量<code>$string</code>的开头, 删除最短匹配<code>$substring</code>的子串</td>
</tr>
<tr>
<td style="text-align:center"><code>${string##substring}</code></td>
<td style="text-align:center">从变量<code>$string</code>的开头, 删除最长匹配<code>$substring</code>的子串</td>
</tr>
<tr>
<td style="text-align:center"><code>${string%substring}</code></td>
<td style="text-align:center">从变量<code>$string</code>的结尾, 删除最短匹配<code>$substring</code>的子串</td>
</tr>
<tr>
<td style="text-align:center"><code>${string%%substring}</code></td>
<td style="text-align:center">从变量<code>$string</code>的结尾, 删除最长匹配<code>$substring</code>的子串</td>
</tr>
<tr>
<td style="text-align:center"><code>${string/substring/replacement}</code></td>
<td style="text-align:center">使用<code>$replacement</code>, 来代替第一个匹配的<code>$substring</code></td>
</tr>
<tr>
<td style="text-align:center"><code>${string//substring/replacement}</code></td>
<td style="text-align:center">使用<code>$replacement</code>, 代替所有匹配的<code>$substring</code></td>
</tr>
<tr>
<td style="text-align:center"><code>${string/#substring/replacement}</code></td>
<td style="text-align:center">如果<code>$string</code>的前缀匹配<code>$substring</code>, 那么就用<code>$replacement</code>来代替匹配到的<code>$substring</code></td>
</tr>
<tr>
<td style="text-align:center"><code>${string/%substring/replacement}</code></td>
<td style="text-align:center">如果<code>$string</code>的后缀匹配<code>$substring</code>, 那么就用<code>$replacement</code>来代替匹配到的<code>$substring</code></td>
</tr>
</tbody>
</table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字符串长度 $&#123;#变量名&#125;得到字符串长度</span></span><br><span class="line">hi=<span class="string">"abcd"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;#hi&#125;</span> <span class="comment">#输出 4</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 截取字串  $&#123;变量名:起始:长度&#125;得到子字符串</span></span><br><span class="line"><span class="built_in">test</span>=<span class="string">'I love china'</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test:5&#125;</span>     <span class="comment"># e china</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test:5:10&#125;</span>  <span class="comment"># e china</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字符串删除</span></span><br><span class="line"><span class="comment"># $&#123;变量名#substring正则表达式&#125;从字符串开头开始配备substring,删除匹配上的表达式</span></span><br><span class="line"><span class="comment"># $&#123;变量名%substring正则表达式&#125;从字符串结尾开始配备substring,删除匹配上的表达式</span></span><br><span class="line"><span class="comment"># 注意：$&#123;test##*/&#125;,$&#123;test%/*&#125; 分别是得到文件名，或者目录地址最简单方法</span></span><br><span class="line"><span class="built_in">test</span>=<span class="string">'c:/windows/boot.ini'</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test#/&#125;</span>      <span class="comment"># c:/windows/boot.ini</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test#*/&#125;</span>     <span class="comment"># windows/boot.ini</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test##*/&#125;</span>    <span class="comment"># boot.ini</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test%/*&#125;</span>     <span class="comment"># c:/windows</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test%%/*&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字符串替换</span></span><br><span class="line"><span class="built_in">test</span>=<span class="string">'c:/windows/boot.ini'</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test/\//\\&#125;</span>      <span class="comment"># c:\windows/boot.ini</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;test//\//\\&#125;</span>     <span class="comment"># c:\windows\boot.ini</span></span><br></pre></td></tr></table></figure>
<h4 id="拼接"><a href="#拼接" class="headerlink" title="拼接"></a>拼接</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">your_name=<span class="string">"runoob"</span></span><br><span class="line"><span class="comment"># 使用双引号拼接</span></span><br><span class="line">greeting=<span class="string">"hello, "</span><span class="variable">$your_name</span><span class="string">" !"</span></span><br><span class="line">greeting_1=<span class="string">"hello, <span class="variable">$&#123;your_name&#125;</span> !"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$greeting</span>  <span class="variable">$greeting_1</span> <span class="comment"># hello, runoob ! hello, runoob !</span></span><br><span class="line"><span class="comment"># 使用单引号拼接</span></span><br><span class="line">greeting_2=<span class="string">'hello, '</span><span class="variable">$your_name</span><span class="string">' !'</span></span><br><span class="line">greeting_3=<span class="string">'hello, $&#123;your_name&#125; !'</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$greeting_2</span>  <span class="variable">$greeting_3</span>   <span class="comment"># hello, runoob ! hello, $&#123;your_name&#125; !</span></span><br></pre></td></tr></table></figure>
<h4 id="子字符串"><a href="#子字符串" class="headerlink" title="子字符串"></a>子字符串</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 提取子字符串</span></span><br><span class="line">string=<span class="string">"runoob is a great site"</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;string:1:4&#125;</span> <span class="comment"># 输出 unoo</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 查找子字符串</span></span><br><span class="line">string=<span class="string">"runoob is a great site"</span></span><br><span class="line"><span class="built_in">echo</span> `expr index <span class="string">"<span class="variable">$string</span>"</span> io`  <span class="comment"># 输出 4</span></span><br></pre></td></tr></table></figure>
<h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">array_name=(value0 value1 value2 value3)</span><br><span class="line"><span class="comment">#或者</span></span><br><span class="line">array_name=(</span><br><span class="line">value0</span><br><span class="line">value1</span><br><span class="line">value2</span><br><span class="line">value3</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 还可以单独定义数组的各个分量：</span></span><br><span class="line">array_name[0]=value0</span><br><span class="line">array_name[1]=value1</span><br><span class="line">array_name[n]=valuen</span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取长度元素 $&#123;数组名[下标]&#125;</span></span><br><span class="line">val=<span class="variable">$&#123;array_name[1]&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用 @ 符号可以获取数组中的所有元素</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$&#123;array_name[@]&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 取得数组元素的个数</span></span><br><span class="line">length=<span class="variable">$&#123;#array_name[@]&#125;</span></span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line">length=<span class="variable">$&#123;#array_name[*]&#125;</span></span><br><span class="line"><span class="comment"># 取得数组单个元素的长度</span></span><br><span class="line">lengthn=<span class="variable">$&#123;#array_name[n]&#125;</span></span><br></pre></td></tr></table></figure>
<h2 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以 # 开头的行就是注释, 会被解释器忽略</span></span><br></pre></td></tr></table></figure>
<h1 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h1><p>脚本通过<code>$n</code>获取参数<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"Shell 传递参数实例！"</span>;</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"执行的文件名：<span class="variable">$0</span>"</span>;</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"第一个参数为：<span class="variable">$1</span>"</span>;</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"第二个参数为：<span class="variable">$2</span>"</span>;</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"第三个参数为：<span class="variable">$3</span>"</span>;</span><br></pre></td></tr></table></figure></p>
<p>执行脚本<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ ./test.sh 1 2 3</span><br><span class="line">Shell 传递参数实例！</span><br><span class="line">执行的文件名：./test.sh</span><br><span class="line">第一个参数为：1</span><br><span class="line">第二个参数为：2</span><br><span class="line">第三个参数为：3</span><br></pre></td></tr></table></figure></p>
<h2 id="特殊参数列表"><a href="#特殊参数列表" class="headerlink" title="特殊参数列表"></a>特殊参数列表</h2><table>
<thead>
<tr>
<th style="text-align:center">变量</th>
<th style="text-align:left">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>$0</code></td>
<td style="text-align:left">是脚本本身的名字</td>
</tr>
<tr>
<td style="text-align:center"><code>$n</code></td>
<td style="text-align:left">添加到sehll的各参数值.<code>$1</code>是第一个参数,<code>$2</code>是第二个参数,以此类推</td>
</tr>
<tr>
<td style="text-align:center"><code>$#</code></td>
<td style="text-align:left">传给脚本的参数个数</td>
</tr>
<tr>
<td style="text-align:center"><code>$@</code></td>
<td style="text-align:left">传给脚本的所有参数的列表, 即被扩展为<code>$1</code> <code>$2</code> <code>$3</code>等</td>
</tr>
<tr>
<td style="text-align:center"><code>$*</code></td>
<td style="text-align:left">以一个单字符串显示所有向脚本传递的参数, 即被扩展成<code>\$1c\$2c\$3</code>, 其中c是IFS的第一个字符</td>
</tr>
<tr>
<td style="text-align:center"><code>$$</code></td>
<td style="text-align:left">脚本运行的当前进程ID号</td>
</tr>
<tr>
<td style="text-align:center"><code>$?</code></td>
<td style="text-align:left">显示最后命令的退出状态, 0表示没有错误, 其他表示有错误</td>
</tr>
<tr>
<td style="text-align:center"><code>$!</code></td>
<td style="text-align:left">脚本最后运行的后台Process的进程id</td>
</tr>
<tr>
<td style="text-align:center"><code>$-</code></td>
<td style="text-align:left">显示Shell使用的当前选项，与set命令功能相同。</td>
</tr>
</tbody>
</table>
<p><code>$*</code> 与 <code>$@</code> 区别：</p>
<ul>
<li>相同点：都是引用所有参数。</li>
<li>不同点：只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3，，则 “ * “ 等价于 “1 2 3”（传递了一个参数），而 “@” 等价于 “1” “2” “3”（传递了三个参数）。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">"-- \$* 演示 ---"</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="string">"$*"</span>; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"-- \$@ 演示 ---"</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="string">"<span class="variable">$@</span>"</span>; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure>
<p>执行脚本，输出结果如下所示<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ ./test.sh 1 2 3</span><br><span class="line">-- $* 演示 ---</span><br><span class="line">1 2 3</span><br><span class="line">-- <span class="variable">$@</span> 演示 ---</span><br><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td></tr></table></figure></p>
<h2 id="getopt-getopts"><a href="#getopt-getopts" class="headerlink" title="getopt getopts"></a>getopt getopts</h2><p><a href="https://www.cnblogs.com/FrankTan/archive/2010/03/01/1634516.html" target="_blank" rel="noopener">Bash Shell中命令行选项/参数处理</a></p>
<h1 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h1><h2 id="算数运算符"><a href="#算数运算符" class="headerlink" title="算数运算符"></a>算数运算符</h2><table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">+</td>
<td>加法  `expr $a + $b` 结果为 30</td>
</tr>
<tr>
<td style="text-align:center">-</td>
<td>减法   `expr $a - $b` 结果为 10</td>
</tr>
<tr>
<td style="text-align:center">*</td>
<td>乘法   `expr $a * $b` 结果为  200</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td>除法, 取整   `expr $b / $a` 结果为 2</td>
</tr>
<tr>
<td style="text-align:center">%</td>
<td>取余  `expr $b % $a` 结果为 0</td>
</tr>
<tr>
<td style="text-align:center">=</td>
<td>赋值   a=$b 将把变量 b 的值赋给 a</td>
</tr>
<tr>
<td style="text-align:center">==</td>
<td>相等, 相同返回true    [ $a == $b ] 返回 false</td>
</tr>
<tr>
<td style="text-align:center">!=</td>
<td>不等, 不同返回false   [ $a != $b ] 返回 true</td>
</tr>
</tbody>
</table>
<p><strong>注意 :</strong></p>
<ul>
<li>条件表达式要放在方括号之间, 并且要有空格, 例如: <code>[$a==$b]</code> 是错误的, 必须写成 <code>[ $a == $b ]</code></li>
<li>乘号(*)前边必须加反斜杠()才能实现乘法运算, 即写成<code>expr $a \* $b</code></li>
<li>MAC 中 shell 的 expr 语法是: <code>$((表达式))</code>, 此处表达式中的<code>*</code>不需要转义符号<code>\</code></li>
</ul>
<p>原生bash不支持简单的数学运算, 但可以通过一下几种方式实现:</p>
<ol>
<li><p><code>let</code></p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var=1;</span><br><span class="line"><span class="built_in">let</span> <span class="string">"var+=1"</span>;</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$var</span></span><br></pre></td></tr></table></figure>
<ul>
<li>let几乎支持所有的运算符</li>
<li>方幂运算应使用“**”</li>
<li>变量在表达式中直接访问，不必加$</li>
<li>一般情况下算数表达式可以不加双引号，但是若表达式中有bash中的关键字则需加上</li>
<li>let后的表达式只能进行整数运算</li>
</ul>
</li>
<li><p><code>(())</code> <strong>推荐</strong><br> <code>(())</code>的使用方法与let关键字完全相同</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var=1;</span><br><span class="line">((var+=1));</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$var</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol start="3">
<li><p><code>$[]</code> </p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var=1;</span><br><span class="line">var=$[<span class="variable">$var</span>+1];</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$var</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>$[]</code>将中括号内的表达式作为数学运算先计算结果再输出</li>
<li>对<code>$[]</code>中的变量进行访问时前面需要加<code>$</code></li>
<li><code>$[]</code>支持的运算符与<code>let</code>相同，但也只支持整数运算</li>
</ul>
</li>
<li><p><code>expr</code></p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var=1;</span><br><span class="line">var=`expr <span class="variable">$var</span> + 1`;</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$var</span></span><br></pre></td></tr></table></figure>
<ul>
<li>expr后的表达式符号间需用空格隔开</li>
<li>expr支持的操作符有： |、&amp;、&lt; &gt;=、&lt;、+、-、*、/、%</li>
<li>expr支持的操作符中在使用时需用\进行转义的有：|、&amp;、&lt; &gt;=、&lt;、*</li>
<li>expr同样只支持整数运算</li>
</ul>
</li>
<li><p><code>bc</code><br> bc是linux下的一个简单计算器，支持浮点数计算，在命令行下输入bc即进入计算器程序，而我们想在程序中直接进行浮点数计算时，利用一个简单的管道即可解决问题。</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var=1;</span><br><span class="line">var=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$var</span>+1"</span>|bc`;</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$var</span></span><br></pre></td></tr></table></figure>
<ul>
<li>bc支持除位操作运算符之外的所有运算符。</li>
<li>bc中要使用scale进行精度设置，如scale=2设置小数点2位精度</li>
</ul>
</li>
<li><p><code>awk</code></p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var=1;</span><br><span class="line">var=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$var</span> 1"</span>|awk <span class="string">'&#123;printf("%g",$1+$2)&#125;'</span>`;</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$var</span></span><br></pre></td></tr></table></figure>
<ul>
<li>awk支持除位操作运算符之外的所有运算符</li>
<li>awk内置有log、sqr、cos、sin等等函数</li>
</ul>
</li>
</ol>
<h2 id="关系运算符"><a href="#关系运算符" class="headerlink" title="关系运算符"></a>关系运算符</h2><p>关系运算符只支持数字, 不支持字符串, 除非字符串的值是数字</p>
<table>
<thead>
<tr>
<th style="text-align:center">表达式</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">-eq</td>
<td>两数值相等(equal)      [ $a -eq $b ] 返回 true</td>
</tr>
<tr>
<td style="text-align:center">-ne</td>
<td>两数值不等(not equal)     [ $a -ne $b ] 返回 true</td>
</tr>
<tr>
<td style="text-align:center">-gt</td>
<td>n1大于n2(greater than)     [ $a -gt $b ] 返回 false</td>
</tr>
<tr>
<td style="text-align:center">-lt</td>
<td>n1小于n2(less than)      [ $a -lt $b ] 返回 true</td>
</tr>
<tr>
<td style="text-align:center">-ge</td>
<td>n1大于等于n2(greater than or equal)     [ $a -ge $b ] 返回 false</td>
</tr>
<tr>
<td style="text-align:center">-le</td>
<td>n1小于等于n2(less than or equal)         [ $a -le $b ] 返回 true</td>
</tr>
</tbody>
</table>
<p>示例:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">a=100</span><br><span class="line">b=100</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> -eq <span class="variable">$b</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'$a -eq $b : a 等于 b'</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'$a -eq $b : a 不等于 b'</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 10 -eq 10: a 等于 b</span></span><br></pre></td></tr></table></figure></p>
<h2 id="布尔运算符"><a href="#布尔运算符" class="headerlink" title="布尔运算符"></a>布尔运算符</h2><table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:left">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">!</td>
<td style="text-align:left">非运算     [ ! false ] 返回 true</td>
</tr>
<tr>
<td style="text-align:center">-o</td>
<td style="text-align:left">或运算     [ $a -lt 20 -o $b -gt 100 ] 返回 true</td>
</tr>
<tr>
<td style="text-align:center">-a</td>
<td style="text-align:left">与运算     [ $a -lt 20 -a $b -gt 100 ] 返回 false</td>
</tr>
</tbody>
</table>
<p>示例:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">a=10</span><br><span class="line">b=20</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> != <span class="variable">$b</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> != <span class="variable">$b</span> : a 不等于 b"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> != <span class="variable">$b</span>: a 等于 b"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># 10 != 20 : a 不等于 b</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> -lt 100 -a <span class="variable">$b</span> -gt 15 ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> 小于 100 且 <span class="variable">$b</span> 大于 15 : 返回 true"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> 小于 100 且 <span class="variable">$b</span> 大于 15 : 返回 false"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># 10 小于 100 且 20 大于 15 : 返回 true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> -lt 5 -o <span class="variable">$b</span> -gt 100 ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> -lt 100 -o <span class="variable">$b</span> -gt 100 : returns true"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> -lt 100 -o <span class="variable">$b</span> -gt 100 : returns false"</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure></p>
<h2 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h2><table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">&amp;&amp;</td>
<td style="text-align:center">AND</td>
</tr>
<tr>
<td style="text-align:center">&vert; &vert;</td>
<td style="text-align:center">OR</td>
</tr>
</tbody>
</table>
<p>示例:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">a=10</span><br><span class="line">b=20</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$a</span> -lt 100 &amp;&amp; <span class="variable">$b</span> -gt 100 ]]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"返回 true"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"返回 false"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># 返回 false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$a</span> -lt 100 || <span class="variable">$b</span> -gt 100 ]]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"返回 true"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"返回 false"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># 返回 true</span></span><br></pre></td></tr></table></figure>
<p><strong>注意使用的是双括号</strong></p>
<h2 id="字符串运算符"><a href="#字符串运算符" class="headerlink" title="字符串运算符"></a>字符串运算符</h2><table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">=</td>
<td style="text-align:center">检测两个字符串是否相等</td>
</tr>
<tr>
<td style="text-align:center">!=</td>
<td style="text-align:center">检测两个字符串是否不相等</td>
</tr>
<tr>
<td style="text-align:center">-z</td>
<td style="text-align:center">检测字符串长度是否为0</td>
</tr>
<tr>
<td style="text-align:center">-n</td>
<td style="text-align:center">检测字符串长度是否不为0</td>
</tr>
<tr>
<td style="text-align:center">str</td>
<td style="text-align:center">检测字符串是否不为空</td>
</tr>
<tr>
<td style="text-align:center"><code>str1 &gt; str2</code></td>
<td style="text-align:center">str1字母顺序是否大于str2, 若大于, 则返回true</td>
</tr>
<tr>
<td style="text-align:center"><code>str1 &lt; str2</code></td>
<td style="text-align:center">str1字母顺序是否小于str2, 若小于, 则返回true</td>
</tr>
</tbody>
</table>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">a=<span class="string">"abc"</span></span><br><span class="line">b=<span class="string">"efg"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> != <span class="variable">$b</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> != <span class="variable">$b</span> : a 不等于 b"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> != <span class="variable">$b</span>: a 等于 b"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># abc != efg : a 不等于 b</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ -z <span class="variable">$a</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"-z <span class="variable">$a</span> : 字符串长度为 0"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"-z <span class="variable">$a</span> : 字符串长度不为 0"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># -z abc : 字符串长度不为 0</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ -n <span class="string">"<span class="variable">$a</span>"</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"-n <span class="variable">$a</span> : 字符串长度不为 0"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"-n <span class="variable">$a</span> : 字符串长度为 0"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># -n abc : 字符串长度不为 0</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> : 字符串不为空"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span> : 字符串为空"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"><span class="comment"># abc : 字符串不为空</span></span><br></pre></td></tr></table></figure>
<h2 id="文件测试运算符"><a href="#文件测试运算符" class="headerlink" title="文件测试运算符"></a>文件测试运算符</h2><p>文件类检测:</p>
<table>
<thead>
<tr>
<th style="text-align:center">表达式</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>-e file</code></td>
<td>文件是否存在(exist)</td>
</tr>
<tr>
<td style="text-align:center"><code>-f file</code></td>
<td>文件是否存在且为普通文件(file)</td>
</tr>
<tr>
<td style="text-align:center"><code>-d file</code></td>
<td>文件是否存在且为目录(directory)</td>
</tr>
<tr>
<td style="text-align:center"><code>-b file</code></td>
<td>文件是否存在且为块设备block device</td>
</tr>
<tr>
<td style="text-align:center"><code>-c file</code></td>
<td>文件是否存在且为字符设备character device</td>
</tr>
<tr>
<td style="text-align:center"><code>-S file</code></td>
<td>文件是否存在且为套接字文件Socket</td>
</tr>
<tr>
<td style="text-align:center"><code>-p file</code></td>
<td>文件是否存在且为命名管道文件FIFO(pipe)</td>
</tr>
<tr>
<td style="text-align:center"><code>-L file</code></td>
<td>文件是否存在且是一个链接文件(Link)</td>
</tr>
<tr>
<td style="text-align:center"><code>-h file</code></td>
<td>文件是否存在且是一个链接文件(Link)</td>
</tr>
</tbody>
</table>
<p>文件属性检测:</p>
<table>
<thead>
<tr>
<th style="text-align:center">表达式</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>-r file</code></td>
<td>文件是否存在且当前用户可读</td>
</tr>
<tr>
<td style="text-align:center"><code>-w file</code></td>
<td>文件是否存在且当前用户可写</td>
</tr>
<tr>
<td style="text-align:center"><code>-x file</code></td>
<td>文件是否存在且当前用户可执行</td>
</tr>
<tr>
<td style="text-align:center"><code>-u file</code></td>
<td>文件是否存在且设置了SUID</td>
</tr>
<tr>
<td style="text-align:center"><code>-g file</code></td>
<td>文件是否存在且设置了SGID</td>
</tr>
<tr>
<td style="text-align:center"><code>-k file</code></td>
<td>文件是否存在且设置了sbit(sticky bit)</td>
</tr>
<tr>
<td style="text-align:center"><code>-s file</code></td>
<td>文件是否存在且大小大于0字节，即用于检测文件是否为非空白文件</td>
</tr>
<tr>
<td style="text-align:center"><code>-N file</code></td>
<td>文件是否存在，且自上次read后是否被modify</td>
</tr>
</tbody>
</table>
<p>示例:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> [ -x <span class="variable">$file</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件可执行"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件不可执行"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ -f <span class="variable">$file</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件为普通文件"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件为特殊文件"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ -s <span class="variable">$file</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件不为空"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件为空"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ -e <span class="variable">$file</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件存在"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"文件不存在"</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure></p>
<p>两个文件之间比较:</p>
<table>
<thead>
<tr>
<th style="text-align:center">表达式</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>file1 -nt file2</code></td>
<td>(newer than)判断file1是否比file2新</td>
</tr>
<tr>
<td style="text-align:center"><code>file1 -ot file2</code></td>
<td>(older than)判断file1是否比file2旧</td>
</tr>
<tr>
<td style="text-align:center"><code>file1 -ef file2</code></td>
<td>(equal file)判断file2与file2是否为同一文件, 可用在判断hard link的判定上</td>
</tr>
</tbody>
</table>
<p>示例:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> /bin</span><br><span class="line"><span class="keyword">if</span> <span class="built_in">test</span> -e ./bash</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'文件已存在!'</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'文件不存在!'</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure></p>
<h1 id="echo-printf-命令"><a href="#echo-printf-命令" class="headerlink" title="echo printf 命令"></a>echo printf 命令</h1><p>echo显示换行:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> -e <span class="string">"OK! \n"</span> <span class="comment"># -e 开启转义</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"It is a test"</span></span><br></pre></td></tr></table></figure></p>
<p>结果:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">OK!</span><br><span class="line"></span><br><span class="line">It is a test</span><br></pre></td></tr></table></figure></p>
<p>echo显示不换行:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> -e <span class="string">"OK! \c"</span> <span class="comment"># -e 开启转义 \c 不换行</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"It is a test"</span></span><br></pre></td></tr></table></figure></p>
<p>结果:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">OK! It is a test</span><br></pre></td></tr></table></figure></p>
<p>默认<code>printf</code>不会像<code>echo</code>自动添加换行符, 我们可以手动添加 <code>\n</code><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">echo</span> <span class="string">"Hello, Shell"</span></span><br><span class="line">Hello, Shell</span><br><span class="line">$ <span class="built_in">printf</span> <span class="string">"Hello, Shell\n"</span></span><br><span class="line">Hello, Shell</span><br></pre></td></tr></table></figure></p>
<p>使用格式替换符:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span> <span class="string">"%-10s %-8s %-4s\n"</span> 姓名 性别 体重kg  </span><br><span class="line"><span class="built_in">printf</span> <span class="string">"%-10s %-8s %-4.2f\n"</span> 郭靖 男 66.1234</span><br><span class="line"><span class="built_in">printf</span> <span class="string">"%-10s %-8s %-4.2f\n"</span> 杨过 男 48.6543</span><br><span class="line"><span class="built_in">printf</span> <span class="string">"%-10s %-8s %-4.2f\n"</span> 郭芙 女 47.9876</span><br></pre></td></tr></table></figure></p>
<p>结果:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">姓名     性别   体重kg</span><br><span class="line">郭靖     男      66.12</span><br><span class="line">杨过     男      48.65</span><br><span class="line">郭芙     女      47.99</span><br></pre></td></tr></table></figure></p>
<p><code>%s</code> <code>%c</code> <code>%d</code> <code>%f</code>都是格式替代符<br><code>%-10s</code> 指一个宽度为10个字符(<code>-</code>表示左对齐, 没有则表示右对齐), 任何字符都会被显示在10个字符宽的字符内, 如果不足则自动以空格填充, 超过也会将内容全部显示出来<br><code>%-4.2f</code> 指格式化为小数, 其中.2指保留2位小数<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># format-string为双引号</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">"%d %s\n"</span> 1 <span class="string">"abc"</span>        </span><br><span class="line"><span class="comment">#1 abc</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 单引号与双引号效果一样</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">'%d %s\n'</span> 1 <span class="string">"abc"</span>        </span><br><span class="line"><span class="comment">#1 abc</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 没有引号也可以输出</span></span><br><span class="line"><span class="built_in">printf</span> %s abcdef            </span><br><span class="line"><span class="comment"># 格式只指定了一个参数，但多出的参数仍然会按照该格式输出，format-string 被重用</span></span><br><span class="line"><span class="built_in">printf</span> %s abc def               </span><br><span class="line"><span class="built_in">printf</span> <span class="string">"%s\n"</span> abc def</span><br><span class="line"><span class="comment">#abcdefabcdefabc</span></span><br><span class="line"><span class="comment">#def</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span> <span class="string">"%s %s %s\n"</span> a b c d e f g h i j</span><br><span class="line"><span class="comment">#a b c</span></span><br><span class="line"><span class="comment">#d e f</span></span><br><span class="line"><span class="comment">#g h i</span></span><br><span class="line"><span class="comment">#j  </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果没有 arguments，那么 %s 用NULL代替，%d 用 0 代替</span></span><br><span class="line"><span class="built_in">printf</span> <span class="string">"%s and %d \n"</span></span><br><span class="line"><span class="comment"># and 0</span></span><br></pre></td></tr></table></figure></p>
<h1 id="test命令"><a href="#test命令" class="headerlink" title="test命令"></a>test命令</h1><p><code>test</code>命令用于检查某个条件是否成立, 它可以进行数字, 字符串和文件三种测试</p>
<p>示例:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 两个整数之间的比较, 支持正负数, 但不支持小数.</span></span><br><span class="line">num1=100</span><br><span class="line">num2=100</span><br><span class="line"><span class="keyword">if</span> <span class="built_in">test</span> $[num1] -eq $[num2]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'两个数相等！'</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'两个数不相等！'</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line">str1=<span class="string">"ru1noob"</span></span><br><span class="line">str2=<span class="string">"runoob"</span></span><br><span class="line"><span class="keyword">if</span> <span class="built_in">test</span> <span class="variable">$str1</span> = <span class="variable">$str2</span></span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'两个字符串相等!'</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'两个字符串不相等!'</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">cd</span> /bin</span><br><span class="line"><span class="keyword">if</span> <span class="built_in">test</span> -e ./notFile -o -e ./bash</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'至少有一个文件存在!'</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">'两个文件都不存在'</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure></p>
<h1 id="各种括号的作用"><a href="#各种括号的作用" class="headerlink" title="各种括号的作用() (()) [] [[]]"></a>各种括号的作用() (()) [] [[]]</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">type</span> [ [[ <span class="built_in">test</span></span><br><span class="line">[ is a shell <span class="built_in">builtin</span></span><br><span class="line">[[ is a shell keyword</span><br><span class="line"><span class="built_in">test</span> is a shell <span class="built_in">builtin</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ <span class="variable">$a</span> != 1 &amp;&amp; <span class="variable">$a</span> != 2 ]]</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> -ne 1] &amp;&amp; [ <span class="variable">$a</span> != 2 ]</span><br><span class="line"><span class="comment"># 或者</span></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> -ne 1 -a <span class="variable">$a</span> != 2 ]</span><br></pre></td></tr></table></figure>
<h2 id="test-和-用法"><a href="#test-和-用法" class="headerlink" title="test 和 [] 用法"></a>test 和 [] 用法</h2><p>它们根据参数的个数来完成测试, 例:</p>
<ul>
<li><p>不带任何参数, 返回false.</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ [  ];<span class="built_in">echo</span> $?</span><br><span class="line">1</span><br></pre></td></tr></table></figure>
</li>
<li><p>只有一个参数时, 仅当参数非空是返回true</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">test</span> <span class="string">""</span>;<span class="built_in">echo</span> $?</span><br><span class="line">1</span><br><span class="line">$ <span class="built_in">test</span> <span class="string">"a"</span>;<span class="built_in">echo</span> $?</span><br><span class="line">0</span><br></pre></td></tr></table></figure>
</li>
<li><p>两个参数时, 有几种情况:</p>
<ul>
<li>第一个参数是单目运算符, 包括文件测试, 如: <code>[ -e file ]</code> 和 <code>[ -n str ]</code></li>
<li>第一个参数是<code>!</code>时, 则只能是<code>[ ! str ]</code>, 等价于<code>[ ! -n str ]</code></li>
<li>第一个参数不是任何有效的操作符, 则直接报错</li>
</ul>
</li>
<li>三个参数时, 也有几种情况:<ul>
<li>使用了双目预运算符, 如: <code>[ file1 -nt file2 ]</code>, <code>[ int1 -eq int2 ]</code></li>
<li>使用了逻辑运算符, 如: <code>[ ! -e file ]</code>, <code>[ ! -z str ]</code></li>
<li>使用了括号, 则只能是<code>[ (string) ]</code></li>
</ul>
</li>
</ul>
<h2 id="用法"><a href="#用法" class="headerlink" title="[[]]用法"></a><code>[[]]</code>用法</h2><p>用法与<code>[]</code>基本相同, 但要注意一下几点:</p>
<ul>
<li><p>当条件表达式中使用<code>==</code>或<code>!=</code>时, 该运算符的右边会被当做pattern被匹配, <code>==</code>表示能匹配成功则返回0, <code>!=</code>则相反. 但只是通配符匹配, 不支持正则. 通配符包括<code>*</code>, <code>?</code> 和 <code>[...]</code></p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ [[ abc == a* ]];<span class="built_in">echo</span> $?</span><br><span class="line">0</span><br><span class="line">$ [[ abc == a*d ]];<span class="built_in">echo</span> $?</span><br><span class="line">1</span><br><span class="line">$ [[ abc == a[bB]c ]];<span class="built_in">echo</span> $?</span><br><span class="line">0</span><br></pre></td></tr></table></figure>
</li>
<li><p>当条件表达式中使用的运算符是”=~”时, 该运算符的右边会被当做正则表达式的pattern被匹配</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ [[ abc =~ aa* ]];<span class="built_in">echo</span> $?</span><br><span class="line">0</span><br><span class="line">$ [[ abc =~ aa.* ]];<span class="built_in">echo</span> $?</span><br><span class="line">1</span><br></pre></td></tr></table></figure>
</li>
<li><p>除了可以使用逻辑运算符<code>!</code>和<code>()</code>, 还可以使用<code>&amp;&amp;</code>, <code>||</code>, 等价于<code>[]</code>的<code>-a</code>和<code>-o</code>, 但是<code>[[]]</code>不在支持<code>-a</code>和<code>-o</code></p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ [[ 3 -eq 3 &amp;&amp; 5 -eq 5 ]];<span class="built_in">echo</span> $?</span><br><span class="line">0</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="使用建议"><a href="#使用建议" class="headerlink" title="使用建议"></a>使用建议</h2><ol>
<li><p>无论是<code>[]</code>还是<code>[[]]</code>, 建议对其中的变量, 字符串使用双引号包裹. 能做字符串比对是, 不要使用数组比较.</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">name=<span class="string">"Tom Hanks"</span></span><br><span class="line">[ <span class="variable">$name</span> = <span class="string">"Tom Hanks"</span> ]</span><br></pre></td></tr></table></figure>
<p> 上面的语句会报错, 因为在变量替换阶段, <code>$name</code>会被替换为Tom Hanks, 但它们没有在引号内, 于是进行单词拆分, 导致等价于执行<code>[ Tom Hanks = &quot;Tom Hanks&quot; ]</code>. 所以应该这么写:</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[ <span class="string">"<span class="variable">$name</span>"</span> = <span class="string">"Tom Hanks"</span> ]</span><br></pre></td></tr></table></figure>
</li>
<li><p>数值比对时, 建议双方都加0, 避免变量为空时报错.</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ [ <span class="variable">$a</span> -eq 7 ]</span><br><span class="line">-bash: [: -eq: unary operator expected</span><br><span class="line"></span><br><span class="line">$ [ $((a+0)) -eq 7 ]; <span class="built_in">echo</span> $?  <span class="comment"># 或者用字符串方式 [ "$a" = "7" ]</span></span><br><span class="line">1</span><br></pre></td></tr></table></figure>
</li>
<li><p>在变量可能为空时, 建议在变量的基础上增加其他辅助字符串. 这比上面的方法更安全</p>
 <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ [ <span class="string">"a<span class="variable">$a</span>"</span> = <span class="string">"a7"</span> ]   <span class="comment"># 判断a是否为7</span></span><br><span class="line">$ [ <span class="string">"a<span class="variable">$a</span>"</span> = <span class="string">"a"</span> ]    <span class="comment"># 判断a是否为空</span></span><br><span class="line">$ [ ! -z <span class="string">"<span class="variable">$a</span>"</span> -a <span class="string">"a<span class="variable">$a</span>"</span> = <span class="string">"a7"</span> ]  <span class="comment"># a不为空且a=7时才为真</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<h1 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h1><h2 id="if-else"><a href="#if-else" class="headerlink" title="if else"></a>if else</h2><p>语法格式如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> commands1</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">    commands2</span><br><span class="line"><span class="keyword">elif</span></span><br><span class="line">    commands3</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    commands4</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure></p>
<p>如果执行<code>commands1</code>后的退出码是0, 则执行<code>then</code>部分的<code>commands2</code>. 退出码非0则执行<code>elif</code>部分的<code>commands3</code>, 以此类推. <code>commands1</code>部分有如下几种写法:</p>
<ul>
<li><code>if [ condition ]</code>: 等价于<code>test</code>, 在所有的POSIX shell上都能用. 注意<code>condition</code>与括号间必须有空格</li>
<li><code>if [[ condition ]]</code>: 基本等价于<code>[]</code>, 但支持更多的条件表达式, 且允许使用逻辑运算符”&amp;&amp;” “||” “!” 和 “()”, 还支持正则表达式匹配. 较新的bash和zsh都支持.</li>
<li><code>if ((condition))</code>:</li>
<li><code>if (command)</code>: 以子shell的方式运行<code>command</code></li>
<li><code>if command</code>: 根据命令执行后的返回码为判断依据</li>
</ul>
<h2 id="for"><a href="#for" class="headerlink" title="for"></a>for</h2><p>语法及示例:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> bar <span class="keyword">in</span> item1 item2 ... itemN</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    command1</span><br><span class="line">    command2</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 写成1行</span></span><br><span class="line"><span class="keyword">for</span> bar <span class="keyword">in</span> item1 item2 ... itemN; <span class="keyword">do</span> command1; command2;.. <span class="keyword">done</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历数字, 4种方式</span></span><br><span class="line"><span class="keyword">for</span> loop <span class="keyword">in</span> 1 2 3 4;</span><br><span class="line"><span class="keyword">for</span> ((loop=1; loop&lt;=4; loop++));</span><br><span class="line"><span class="keyword">for</span> loop <span class="keyword">in</span> $(seq 1 4);</span><br><span class="line"><span class="keyword">for</span> loop <span class="keyword">in</span> &#123;1..4&#125;;</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"value is: <span class="variable">$loop</span>"</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="comment"># 输出</span></span><br><span class="line">value is: 1</span><br><span class="line">value is: 2</span><br><span class="line">value is: 3</span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历字符串</span></span><br><span class="line">str=<span class="string">"This is a string"</span></span><br><span class="line"><span class="keyword">for</span> s <span class="keyword">in</span> <span class="variable">$str</span>; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$s</span> <span class="string">"--"</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="comment"># 输出</span></span><br><span class="line">This --</span><br><span class="line">is --</span><br><span class="line">a --</span><br><span class="line">string --</span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历命令输出</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `ls`;  </span><br><span class="line"><span class="keyword">do</span>   </span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$i</span> is file name\! ;  </span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历文件夹下文件</span></span><br><span class="line"><span class="keyword">for</span> file <span class="keyword">in</span> ./*; <span class="keyword">do</span></span><br><span class="line">    <span class="keyword">if</span> [ -f <span class="variable">$file</span> ]; <span class="keyword">then</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="variable">$file</span> <span class="string">"是文件"</span></span><br><span class="line">    <span class="keyword">elif</span> [ -d <span class="variable">$file</span> ]; <span class="keyword">then</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="variable">$file</span> <span class="string">"是目录"</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="variable">$file</span></span><br><span class="line">    <span class="keyword">fi</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 无限循环</span></span><br><span class="line"><span class="keyword">for</span> ((;;)); <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历目录, 创建子文件夹, 适合直接在命令行执行</span></span><br><span class="line"><span class="built_in">dirs</span>=($(ls /tmp/logs/ | grep ailegal)); <span class="keyword">for</span> dir <span class="keyword">in</span> <span class="variable">$&#123;dirs[@]&#125;</span>; <span class="keyword">do</span> mkdir -p <span class="string">"/tmp/logs/<span class="variable">$&#123;dir&#125;</span>/gc/"</span>; <span class="keyword">done</span></span><br></pre></td></tr></table></figure>
<h2 id="while"><a href="#while" class="headerlink" title="while"></a>while</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> condition</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 循环5次</span></span><br><span class="line">i=0</span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$i</span> -lt 5 ]; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$i</span></span><br><span class="line">    i=$((<span class="variable">$i</span>+1))</span><br><span class="line"><span class="keyword">done</span></span><br><span class="line">0</span><br><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line"></span><br><span class="line"><span class="comment"># 无限循环</span></span><br><span class="line"><span class="keyword">while</span> ((1)); <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="comment"># 方式2</span></span><br><span class="line"><span class="keyword">while</span> [ 1 ]; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="comment"># 方式3</span></span><br><span class="line"><span class="keyword">while</span> :; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"><span class="comment"># 方式4</span></span><br><span class="line"><span class="keyword">while</span> <span class="literal">true</span>; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取文件</span></span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> line; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$line</span></span><br><span class="line"><span class="keyword">done</span> &lt; <span class="string">"/etc/passwd"</span></span><br><span class="line"><span class="comment"># 也可以这样写</span></span><br><span class="line">cat /etc/passwd | &#123;</span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> line; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$line</span></span><br><span class="line"><span class="keyword">done</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="until"><a href="#until" class="headerlink" title="until"></a>until</h2><p>语法格式:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">until condition</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line"><span class="keyword">then</span></span><br></pre></td></tr></table></figure></p>
<p><code>until</code>循环执行一系列命令, 直到<code>condition</code>为<code>true</code>时停止. 和<code>while</code>循环刚好相反.<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line">a=0</span><br><span class="line"></span><br><span class="line">until [ ! <span class="variable">$a</span> -lt 10 ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="variable">$a</span></span><br><span class="line">   a=`expr <span class="variable">$a</span> + 1`</span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure></p>
<h2 id="case"><a href="#case" class="headerlink" title="case"></a>case</h2><p>语法格式:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> 值 <span class="keyword">in</span></span><br><span class="line">模式1)</span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line">    ;;</span><br><span class="line">模式2)</span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line">    ;;</span><br><span class="line">*)</span><br><span class="line">    <span class="built_in">command</span></span><br><span class="line">    ;;        </span><br><span class="line"><span class="keyword">esac</span></span><br></pre></td></tr></table></figure></p>
<p>示例:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">'输入 1 到 3 之间的数字:'</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">'你输入的数字为:'</span></span><br><span class="line"><span class="built_in">read</span> aNum</span><br><span class="line"><span class="keyword">case</span> <span class="variable">$aNum</span> <span class="keyword">in</span></span><br><span class="line">    1)  <span class="built_in">echo</span> <span class="string">'你选择了 1'</span></span><br><span class="line">    ;;</span><br><span class="line">    2)  <span class="built_in">echo</span> <span class="string">'你选择了 2'</span></span><br><span class="line">    ;;</span><br><span class="line">    3)  <span class="built_in">echo</span> <span class="string">'你选择了 3'</span></span><br><span class="line">    ;;</span><br><span class="line">    *)  <span class="built_in">echo</span> <span class="string">'你没有输入 1 到 4 之间的数字'</span></span><br><span class="line">    ;;</span><br><span class="line"><span class="keyword">esac</span></span><br><span class="line"></span><br><span class="line">name=`basename <span class="variable">$0</span> .sh`</span><br><span class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></span><br><span class="line"> s|start)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">"start..."</span></span><br><span class="line">        ;;</span><br><span class="line"> stop)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">"stop ..."</span></span><br><span class="line">        ;;</span><br><span class="line"> reload)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">"reload..."</span></span><br><span class="line">        ;;</span><br><span class="line"> *)</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$name</span> [start|stop|reload]"</span></span><br><span class="line">        <span class="built_in">exit</span> 1</span><br><span class="line">        ;;</span><br><span class="line"><span class="keyword">esac</span></span><br><span class="line"><span class="built_in">exit</span> 0</span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断输入文件是文件还是目录</span></span><br><span class="line">option=<span class="string">"<span class="variable">$&#123;1&#125;</span>"</span>  </span><br><span class="line"><span class="keyword">case</span> <span class="variable">$&#123;option&#125;</span> <span class="keyword">in</span>   </span><br><span class="line">    -f) file=<span class="string">"<span class="variable">$&#123;2&#125;</span>"</span>  </span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"file name is <span class="variable">$file</span>"</span>  </span><br><span class="line">    ;;  </span><br><span class="line">    -d) dir=<span class="string">"<span class="variable">$&#123;2&#125;</span>"</span>  </span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"dir name is <span class="variable">$dir</span>"</span>  </span><br><span class="line">    ;;  </span><br><span class="line">    *)<span class="built_in">echo</span> <span class="string">"basename <span class="variable">$&#123;0&#125;</span> :usage:[-f file ]| [-d  directory]"</span>  </span><br><span class="line">    <span class="built_in">exit</span> 1  </span><br><span class="line">    ;;  </span><br><span class="line"><span class="keyword">esac</span></span><br></pre></td></tr></table></figure></p>
<p>注意:</p>
<ul>
<li><code>*)</code>相当于其他语言中的<code>default</code></li>
<li>除了<code>*)</code>模式, 其他分支中的<code>;;</code>是必须的, 相当于其他语言中的<code>break</code></li>
<li><code>|</code>分隔多个模式, 相当于<code>or</code></li>
</ul>
<h2 id="break-continue"><a href="#break-continue" class="headerlink" title="break continue"></a>break continue</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> :</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> -n <span class="string">"输入 1 到 5 之间的数字:"</span></span><br><span class="line">    <span class="built_in">read</span> aNum</span><br><span class="line">    <span class="keyword">case</span> <span class="variable">$aNum</span> <span class="keyword">in</span></span><br><span class="line">        1|2|3|4|5) <span class="built_in">echo</span> <span class="string">"你输入的数字为 <span class="variable">$aNum</span>!"</span></span><br><span class="line">        ;;</span><br><span class="line">        *) <span class="built_in">echo</span> <span class="string">"你输入的数字不是 1 到 5 之间的! 游戏结束"</span></span><br><span class="line">            <span class="built_in">break</span></span><br><span class="line">        ;;</span><br><span class="line">    <span class="keyword">esac</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure>
<p>跳出多次循环时使用<code>break n</code>, n表示要跳出的循环层数, 默认为1.<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">a=1</span><br><span class="line"><span class="keyword">while</span> [ <span class="variable">$a</span> -le 5 ]</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"Outer loop:a=<span class="variable">$a</span>"</span></span><br><span class="line">   a=$[<span class="variable">$a</span>+1]</span><br><span class="line">   <span class="keyword">for</span> val <span class="keyword">in</span> 1 2 3 4 5</span><br><span class="line">   <span class="keyword">do</span></span><br><span class="line">        <span class="keyword">if</span> [ <span class="variable">$val</span> -eq 3 ]</span><br><span class="line">        <span class="keyword">then</span></span><br><span class="line">            <span class="built_in">break</span> 2</span><br><span class="line">        <span class="keyword">fi</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="string">"	Inner loop:val=<span class="variable">$val</span>"</span></span><br><span class="line">        val=$[<span class="variable">$val</span>+1]</span><br><span class="line">   <span class="keyword">done</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure></p>
<p>continue仅跳出本次循环<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a=1</span><br><span class="line"><span class="keyword">for</span> val <span class="keyword">in</span> 1 2 3 4 5</span><br><span class="line"><span class="keyword">do</span></span><br><span class="line">  <span class="keyword">if</span> [ <span class="variable">$val</span> -le 3 ]</span><br><span class="line">  <span class="keyword">then</span></span><br><span class="line">       <span class="built_in">continue</span></span><br><span class="line">  <span class="keyword">fi</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"val=<span class="variable">$val</span>"</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure></p>
<h1 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h1><p>函数定义格式如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[<span class="keyword">function</span>] funcName [()]</span><br><span class="line">&#123;</span><br><span class="line">    //<span class="keyword">do</span> something</span><br><span class="line">    [<span class="built_in">return</span> int;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>说明:</p>
<ul>
<li>可以使用<code>function foo()</code>定义, 也可以直接<code>foo()</code>定义</li>
<li>可以加<code>return xx;</code>返回, xx的值为0-255. 如果没有return, 将以最后一条命令运行结果作为返回值.</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="function"><span class="title">foo</span></span>() &#123;</span><br><span class="line">    <span class="built_in">return</span> $((1 + 2));</span><br><span class="line">&#125;</span><br><span class="line">foo</span><br><span class="line"><span class="built_in">echo</span> <span class="string">"函数返回值为 $?"</span></span><br></pre></td></tr></table></figure>
<h2 id="函数参数"><a href="#函数参数" class="headerlink" title="函数参数"></a>函数参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">paramFunc</span></span>() &#123;</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"第一个参数为 <span class="variable">$1</span> !"</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"第二个参数为 <span class="variable">$2</span> !"</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"第十个参数为 <span class="variable">$10</span> !"</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"第十个参数为 <span class="variable">$&#123;10&#125;</span> !"</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"第十一个参数为 <span class="variable">$&#123;11&#125;</span> !"</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"参数总数有 <span class="variable">$#</span> 个!"</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"作为一个字符串输出所有参数 $* !"</span></span><br><span class="line">&#125;</span><br><span class="line">paramFunc 1 2 3 4 5 6 7 8 9 34 73</span><br></pre></td></tr></table></figure>
<p>输出:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">第一个参数为 1 !</span><br><span class="line">第二个参数为 2 !</span><br><span class="line">第十个参数为 10 !</span><br><span class="line">第十个参数为 34 !</span><br><span class="line">第十一个参数为 73 !</span><br><span class="line">参数总数有 11 个!</span><br><span class="line">作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !</span><br></pre></td></tr></table></figure></p>
<p>注意: <strong>当n&gt;=10时, 需要使用${n}来获取参数</strong>.</p>
<p>当脚本有main函数, 你希望脚本运行时传递的所有命令行参数都能被main函数捕捉到, 可以这么写:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/sh</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">main</span></span>() &#123;</span><br><span class="line">    <span class="keyword">for</span> p <span class="keyword">in</span> <span class="string">"<span class="variable">$&#123;@&#125;</span>"</span>; <span class="keyword">do</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="variable">$p</span>;</span><br><span class="line">    <span class="keyword">done</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">main <span class="string">"<span class="variable">$&#123;@&#125;</span>"</span></span><br></pre></td></tr></table></figure></p>
<p>执行结果:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ ./test.sh a 12 -x</span><br><span class="line">a</span><br><span class="line">12</span><br><span class="line">-x</span><br></pre></td></tr></table></figure></p>
<p>同理, 当A函数调用B函数时, 希望把调用A时传递的所有参数都传递给B, 也可以这么写<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">A</span></span>() &#123;</span><br><span class="line">    B <span class="string">"<span class="variable">$&#123;@&#125;</span>"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">B</span></span>() &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h1 id="输入-输出重定向"><a href="#输入-输出重定向" class="headerlink" title="输入/输出重定向"></a>输入/输出重定向</h1><p>一个命令通常从标准输入读取输入, 并将其输出写入标准输出. 通常都是终端. 重定向命令列表如下:</p>
<table>
<thead>
<tr>
<th style="text-align:center">命令</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">command &gt; file</td>
<td style="text-align:center">将输出重定向到file</td>
</tr>
<tr>
<td style="text-align:center">command &lt; file</td>
<td style="text-align:center">将输入重定向到file</td>
</tr>
<tr>
<td style="text-align:center">command &gt;&gt; file</td>
<td style="text-align:center">将输出已追加方式重定向到file</td>
</tr>
<tr>
<td style="text-align:center">n &gt; file</td>
<td style="text-align:center">将文件描述符为n的文件重定向到file</td>
</tr>
<tr>
<td style="text-align:center">n &gt;&gt; file</td>
<td style="text-align:center">将文件描述符为n的文件已追加方式重定向到file</td>
</tr>
<tr>
<td style="text-align:center">n &gt;&amp; m</td>
<td style="text-align:center">将输出文件m和n合并</td>
</tr>
<tr>
<td style="text-align:center">n &lt;&amp; m</td>
<td style="text-align:center">将输入文件m和n合并</td>
</tr>
<tr>
<td style="text-align:center">&lt;&lt; tag</td>
<td style="text-align:center">将开始标记tag和结束标记tag之间的内容作为输入</td>
</tr>
</tbody>
</table>
<blockquote>
<p>文件描述符 0 通常是标准输入(STDIN), 1 是标准输出(STDOUT), 2 是标准错误输出(STDERR)</p>
</blockquote>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">$ who &gt; users.txt</span><br><span class="line">$ cat users.txt</span><br><span class="line">tonnyyi  console  Dec 28 09:17</span><br><span class="line">tonnyyi  ttys000  Dec 28 09:28</span><br><span class="line">tonnyyi  ttys004  Dec 29 14:44</span><br><span class="line"></span><br><span class="line"><span class="comment"># 输出重定向会覆盖文件内容, 如果不希望被覆盖, 可以使用追加的方式</span></span><br><span class="line">$ <span class="built_in">echo</span> <span class="string">"hello"</span> &gt;&gt; users.txt</span><br><span class="line">$ cat users.txt</span><br><span class="line">tonnyyi  console  Dec 28 09:17</span><br><span class="line">tonnyyi  ttys000  Dec 28 09:28</span><br><span class="line">tonnyyi  ttys004  Dec 29 14:44</span><br><span class="line">hello</span><br><span class="line"></span><br><span class="line"><span class="comment"># 输入重定向可以将原本需要从键盘获取输入的命令转移到从文件读取</span></span><br><span class="line">$ grep tonnyyi &lt; user.txt</span><br><span class="line">tonnyyi  console  Dec 28 09:17</span><br><span class="line">tonnyyi  ttys000  Dec 28 09:28</span><br><span class="line">tonnyyi  ttys004  Dec 29 14:44</span><br><span class="line"></span><br><span class="line"><span class="comment"># 同时替换输入和输出, 执行command, 从infile读取内容, 然后将输出写入到outfile</span></span><br><span class="line">$ <span class="built_in">command</span> &lt; infile &gt; outfile</span><br></pre></td></tr></table></figure>
<p>一般情况下, 每个Unix/Linux命令运行时都会打开3个文件</p>
<ul>
<li>标准输入文件(stdin): 其文件描述符为<strong>0</strong>, Unix程序默认从中读取数据</li>
<li>标准输出文件(stdout): 其文件描述符为<strong>1</strong>, Unix程序默认向其输出数据</li>
<li>标准错误文件(stderr): 其文件描述符为<strong>2</strong>, Unix程序会向stderr流中写入错误信息</li>
</ul>
<p>如果希望stderr重定向到file, 可以这样写:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">command</span> 2 &gt; file</span><br></pre></td></tr></table></figure></p>
<p>如果希望将stdout和stderr合并后重定向到file, 可以这样写:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">command</span> &gt; file 2&gt;<span class="variable">$1</span></span><br></pre></td></tr></table></figure></p>
<h2 id="dev-null-文件"><a href="#dev-null-文件" class="headerlink" title="/dev/null 文件"></a>/dev/null 文件</h2><p><code>/dev/null</code>是一个特殊文件, 写入到它的内容都会被丢弃; 如果尝试从该文件读取内容, 则什么也读不到. 将命令的输出重定向到它, 会起到”禁止输出”的效果. 如果希望屏蔽stdout和stderr, 可以这样写:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">command</span> &gt; /dev/null 2&gt;&amp;1</span><br></pre></td></tr></table></figure></p>
<h1 id="文件包含"><a href="#文件包含" class="headerlink" title="文件包含"></a>文件包含</h1><p>和其他语言一样, Shell 也可以包含外部脚本, 这样可以很方便的封装一些公用的代码作为一个独立的文件. 语法格式如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">. filename      <span class="comment"># 点号与文件名中间有一个空格</span></span><br><span class="line">或</span><br><span class="line"><span class="built_in">source</span> filename</span><br></pre></td></tr></table></figure></p>
<h2 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h2><p><code>test1.sh</code>内容如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line">url=<span class="string">"http://www.baidu.com"</span></span><br></pre></td></tr></table></figure></p>
<p><code>test2.sh</code>内容如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line">. ./test1.sh   <span class="comment"># 或者 source ./test1.sh</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"百度地址: <span class="variable">$url</span>"</span></span><br></pre></td></tr></table></figure></p>
<h1 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h1><h2 id="遍历命令结果"><a href="#遍历命令结果" class="headerlink" title="遍历命令结果"></a>遍历命令结果</h2><p>字段分隔符(Internal Field Separator, <code>IFS</code>)是shell脚本中的一个重要概念, 处理文本数据的时候非常的有用, 是把单个数据流划分成不同数据元素的定界符. 系统环境默认的IFS是空白字符(换行符, 制表符或者空格)<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$ jps -l</span><br><span class="line">88483 ailegal-case.jar</span><br><span class="line">85666 ailegal-center.jar</span><br><span class="line">85797 ailegal-gateway.jar</span><br><span class="line"></span><br><span class="line">pros=($(jps -l))</span><br><span class="line"><span class="keyword">for</span> pro <span class="keyword">in</span> <span class="variable">$&#123;pros[@]&#125;</span>; <span class="keyword">do</span></span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$pro</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure></p>
<p>输出:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">88483</span><br><span class="line">ailegal-case.jar</span><br><span class="line">85666</span><br><span class="line">ailegal-center.jar</span><br><span class="line">85797</span><br><span class="line">ailegal-gateway.jar</span><br></pre></td></tr></table></figure></p>
<p>如果想以换行符作为分隔符, 则可以这么做:<br>在Bash 4.0(<code>bash -version</code>)之前:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">ls -l | <span class="keyword">while</span> <span class="built_in">read</span> x; <span class="keyword">do</span> <span class="built_in">echo</span> <span class="variable">$x</span>; <span class="keyword">done</span></span><br><span class="line"><span class="comment">#或者</span></span><br><span class="line">(IFS=<span class="string">'</span></span><br><span class="line"><span class="string">'</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> `ls -l`; <span class="keyword">do</span> <span class="built_in">echo</span> <span class="variable">$x</span>; <span class="keyword">done</span>)</span><br><span class="line"><span class="comment">#或者</span></span><br><span class="line"><span class="keyword">while</span> <span class="built_in">read</span> x; <span class="keyword">do</span> <span class="built_in">echo</span> <span class="variable">$x</span>; <span class="keyword">done</span> &lt;&lt; EOF</span><br><span class="line">$(ls -l)</span><br><span class="line">EOF</span><br></pre></td></tr></table></figure></p>
<p>如果是在函数内, 可以使用<code>local</code>命令, 避免全局性的修改IFS<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/sh</span></span><br><span class="line"><span class="function"><span class="title">test</span></span>() &#123;</span><br><span class="line">   <span class="built_in">local</span> IFS=$<span class="string">'\n'</span></span><br><span class="line">   <span class="built_in">local</span> lines=$(ls -l)</span><br><span class="line">   <span class="keyword">for</span> line <span class="keyword">in</span> <span class="variable">$lines</span>; <span class="keyword">do</span></span><br><span class="line">       <span class="built_in">echo</span> <span class="variable">$line</span></span><br><span class="line">   <span class="keyword">done</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">test</span></span><br></pre></td></tr></table></figure></p>
<p>4.0以后可以使用<code>mapfile</code>:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mapfile</span> -t files &lt; &lt;(ls -l)</span><br><span class="line"><span class="keyword">for</span> file <span class="keyword">in</span> <span class="string">"<span class="variable">$&#123;files[@]&#125;</span>"</span>; <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="variable">$file</span></span><br><span class="line"><span class="keyword">done</span></span><br></pre></td></tr></table></figure></p>
<h4 id="生成随机数"><a href="#生成随机数" class="headerlink" title="生成随机数"></a>生成随机数</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成1个, 范围2000 ~ 6500</span></span><br><span class="line">shuf -i 2000-6500 -n 1</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成10个范围在0~100的随机数并写入文件</span></span><br><span class="line">shuf -i 0-100 -n 10 -i random_number.txt</span><br><span class="line"></span><br><span class="line"><span class="comment"># 乱序读取文件每一行</span></span><br><span class="line">shuf foo.txt</span><br></pre></td></tr></table></figure>
<h4 id="进程休眠sleep"><a href="#进程休眠sleep" class="headerlink" title="进程休眠sleep"></a>进程休眠sleep</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">sleep 0.5</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 休眠3秒</span></span><br><span class="line">sleep 3</span><br><span class="line">sleep 3s</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 休眠3分钟</span></span><br><span class="line">sleep 3m</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 休眠3小时</span></span><br><span class="line">sleep 3h</span><br></pre></td></tr></table></figure>

      
    </div>

    

    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Tonny Yi</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://codertang.com/2019/01/10/shell/" title="shell">http://codertang.com/2019/01/10/shell/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc/4.0/" rel="external nofollow" target="_blank">CC BY-NC 4.0</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/linux/" rel="tag"># linux</a>
          
            <a href="/tags/shell/" rel="tag"># shell</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/12/24/jdk8-datetime/" rel="next" title="jdk8时间日期">
                <i class="fa fa-chevron-left"></i> jdk8时间日期
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/02/21/hexo-drawing-plantuml/" rel="prev" title="在hexo中画图-PlantUml">
                在hexo中画图-PlantUml <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.png" alt="Tonny Yi">
            
              <p class="site-author-name" itemprop="name">Tonny Yi</p>
              <p class="site-description motion-element" itemprop="description">Coding, Life, Zen</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">49</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">42</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">59</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/tonnyyi" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:tonnyemails@gmail.com" target="_blank" title="E-Mail"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc/4.0/" class="cc-opacity" target="_blank">
                <img src="/images/cc-by-nc.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#基本语法"><span class="nav-number">1.</span> <span class="nav-text">基本语法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#echo-命令"><span class="nav-number">1.1.</span> <span class="nav-text">echo 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用参数"><span class="nav-number">1.2.</span> <span class="nav-text">常用参数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#n"><span class="nav-number">1.2.1.</span> <span class="nav-text">-n</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#e"><span class="nav-number">1.3.</span> <span class="nav-text">-e</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#变量"><span class="nav-number">2.</span> <span class="nav-text">变量</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#声明变量"><span class="nav-number">2.1.</span> <span class="nav-text">声明变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#只读变量"><span class="nav-number">2.2.</span> <span class="nav-text">只读变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除变量"><span class="nav-number">2.3.</span> <span class="nav-text">删除变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量替换"><span class="nav-number">2.4.</span> <span class="nav-text">变量替换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量类型"><span class="nav-number">2.5.</span> <span class="nav-text">变量类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符串"><span class="nav-number">2.6.</span> <span class="nav-text">字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#操作"><span class="nav-number">2.6.1.</span> <span class="nav-text">操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#拼接"><span class="nav-number">2.6.1.1.</span> <span class="nav-text">拼接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#子字符串"><span class="nav-number">2.6.1.2.</span> <span class="nav-text">子字符串</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组"><span class="nav-number">2.7.</span> <span class="nav-text">数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注释"><span class="nav-number">2.8.</span> <span class="nav-text">注释</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参数"><span class="nav-number">3.</span> <span class="nav-text">参数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#特殊参数列表"><span class="nav-number">3.1.</span> <span class="nav-text">特殊参数列表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#getopt-getopts"><span class="nav-number">3.2.</span> <span class="nav-text">getopt getopts</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#运算符"><span class="nav-number">4.</span> <span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#算数运算符"><span class="nav-number">4.1.</span> <span class="nav-text">算数运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关系运算符"><span class="nav-number">4.2.</span> <span class="nav-text">关系运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#布尔运算符"><span class="nav-number">4.3.</span> <span class="nav-text">布尔运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#逻辑运算符"><span class="nav-number">4.4.</span> <span class="nav-text">逻辑运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符串运算符"><span class="nav-number">4.5.</span> <span class="nav-text">字符串运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#文件测试运算符"><span class="nav-number">4.6.</span> <span class="nav-text">文件测试运算符</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#echo-printf-命令"><span class="nav-number">5.</span> <span class="nav-text">echo printf 命令</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#test命令"><span class="nav-number">6.</span> <span class="nav-text">test命令</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#各种括号的作用"><span class="nav-number">7.</span> <span class="nav-text">各种括号的作用() (()) [] [[]]</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#test-和-用法"><span class="nav-number">7.1.</span> <span class="nav-text">test 和 [] 用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#用法"><span class="nav-number">7.2.</span> <span class="nav-text">[[]]用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用建议"><span class="nav-number">7.3.</span> <span class="nav-text">使用建议</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#流程控制"><span class="nav-number">8.</span> <span class="nav-text">流程控制</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#if-else"><span class="nav-number">8.1.</span> <span class="nav-text">if else</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#for"><span class="nav-number">8.2.</span> <span class="nav-text">for</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#while"><span class="nav-number">8.3.</span> <span class="nav-text">while</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#until"><span class="nav-number">8.4.</span> <span class="nav-text">until</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#case"><span class="nav-number">8.5.</span> <span class="nav-text">case</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#break-continue"><span class="nav-number">8.6.</span> <span class="nav-text">break continue</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#函数"><span class="nav-number">9.</span> <span class="nav-text">函数</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#函数参数"><span class="nav-number">9.1.</span> <span class="nav-text">函数参数</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#输入-输出重定向"><span class="nav-number">10.</span> <span class="nav-text">输入/输出重定向</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#实例"><span class="nav-number">10.1.</span> <span class="nav-text">实例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#dev-null-文件"><span class="nav-number">10.2.</span> <span class="nav-text">/dev/null 文件</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件包含"><span class="nav-number">11.</span> <span class="nav-text">文件包含</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#实例-1"><span class="nav-number">11.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#其他"><span class="nav-number">12.</span> <span class="nav-text">其他</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#遍历命令结果"><span class="nav-number">12.1.</span> <span class="nav-text">遍历命令结果</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#生成随机数"><span class="nav-number">12.1.0.1.</span> <span class="nav-text">生成随机数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#进程休眠sleep"><span class="nav-number">12.1.0.2.</span> <span class="nav-text">进程休眠sleep</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tonny Yi</span>

  

  
</div>


  <script src="https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js"></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize("");
    }
  </script>





  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://theme-next.org">NexT.Mist</a></div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



	





  





  










  





  

  

  

  

  
  

  

  

  

  

  
  <style>
    .copy-btn {
      display: inline-block;
      padding: 6px 12px;
      font-size: 13px;
      font-weight: 700;
      line-height: 20px;
      color: #333;
      white-space: nowrap;
      vertical-align: middle;
      cursor: pointer;
      background-color: #eee;
      background-image: linear-gradient(#fcfcfc, #eee);
      border: 1px solid #d5d5d5;
      border-radius: 3px;
      user-select: none;
      outline: 0;
    }

    .highlight-wrap .copy-btn {
      transition: opacity .3s ease-in-out;
      opacity: 0;
      padding: 2px 6px;
      position: absolute;
      right: 4px;
      top: 8px;
    }

    .highlight-wrap:hover .copy-btn,
    .highlight-wrap .copy-btn:focus {
      opacity: 1
    }

    .highlight-wrap {
      position: relative;
    }
  </style>
  <script>
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function (e) {
        var code = $(this).parent().find('.code').find('.line').map(function (i, e) {
          return $(e).text()
        }).toArray().join('\n')
        var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
        
          if(result)$(this).text('复制成功')
          else $(this).text('复制失败')
        
        $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('复制')
        }, 300)
      }).append(e)
    })
  </script>


</body>
</html>
